﻿Public Class frmPago
    Public IdDetalle As Integer
    Public Abono As Double

    Private Sub btnRealizarPago_Click(sender As Object, e As EventArgs) Handles btnRealizarPago.Click
        If Me.txtPagoRecibido.EditValue <> Nothing Then
            If Me.txtTotalPagar.EditValue = 0 Then
                'GuardarPagoNormal()
            Else
                If Me.txtPagoRecibido.EditValue = Me.txtTotalPagar.EditValue Then
                    GuardarPagoNormal()
                ElseIf Me.txtPagoRecibido.EditValue > Me.txtTotalPagar.EditValue Then
                    GuardarPagoMayor()
                ElseIf Me.txtPagoRecibido.EditValue < Me.txtTotalPagar.EditValue Then
                    GuardarPagoMenor()
                End If
            End If
        Else
            Me.txtPagoRecibido.BackColor = Color.LemonChiffon
            MessageBox.Show("Ingrese el pago recibido por el cliente", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.txtPagoRecibido.Focus()
        End If
    End Sub

    Private Sub txtPagoRecibido_EditValueChanged(sender As Object, e As EventArgs) Handles txtPagoRecibido.EditValueChanged
        Me.txtPagoRecibido.BackColor = Color.White
    End Sub

    Private Sub GuardarPagoNormal()
        Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoNormal(Me.dtpFechaPago.DateTime.Date, Me.txtPagoRecibido.EditValue, IdDetalle)
        MessageBox.Show("Se ha realizado correctamente el pago", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        frmControlCreditosVigentes.ActualizarGrilla()
        Me.Dispose()
        Me.Close()
    End Sub

    Private Sub GuardarPagoMayor()
        Dim MontoPagado As Double = Me.txtPagoRecibido.EditValue
        Dim CuotaAPagar As Double = Me.txtCuotaMensual.EditValue
        Dim TotalPagar As Double = Me.txtTotalPagar.EditValue
        Dim PagoAparcial As Double = Me.txtAbono.EditValue + Me.txtTotalPagar.EditValue
        Dim IdSiguientePago As Integer = 0

        MontoPagado = MontoPagado - TotalPagar
        If Me.txtAbono.EditValue > 0 Then
            Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoNormal(Me.dtpFechaPago.DateTime.Date, PagoAparcial, IdDetalle)
        Else
            Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoNormal(Me.dtpFechaPago.DateTime.Date, TotalPagar, IdDetalle)
        End If

        IdSiguientePago = IdDetalle + 1

        Do
            If MontoPagado >= CuotaAPagar Then
                Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoNormal(Me.dtpFechaPago.DateTime.Date, CuotaAPagar, IdSiguientePago)
                MontoPagado = MontoPagado - CuotaAPagar
            Else
                Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoMenor(Me.dtpFechaPago.DateTime.Date, MontoPagado, IdSiguientePago)
                MontoPagado = 0
            End If

            IdSiguientePago += 1

        Loop While MontoPagado > 0

        MessageBox.Show("Se ha realizado correctamente el pago", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        frmControlCreditosVigentes.ActualizarGrilla()
        Me.Dispose()
        Me.Close()

    End Sub

    Private Sub GuardarPagoMenor()
        Dim MontoPagado As Double = Me.txtPagoRecibido.EditValue
        If Me.txtAbono.EditValue < 0.1 Then
            MontoPagado = MontoPagado - (Me.txtInteresMora.EditValue - Me.txtNotaCredito.EditValue) + Me.txtNotaDebito.EditValue
        Else
            Dim AbonoViejo As Double = Abono

            'MontoPagado += Abono
            MontoPagado = MontoPagado - (Me.txtInteresMora.EditValue - Me.txtNotaCredito.EditValue) + Me.txtNotaDebito.EditValue + AbonoViejo
        End If

        Me.DetallePlanPagoTableAdapter1.SP_GuardarPagoMenor(Me.dtpFechaPago.DateTime.Date, MontoPagado, IdDetalle)

        MessageBox.Show("Se ha realizado correctamente el pago", varTitulo, MessageBoxButtons.OK, MessageBoxIcon.Information)
        frmControlCreditosVigentes.ActualizarGrilla()
        Me.Dispose()
        Me.Close()
    End Sub

    Private Sub frmPago_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.dtpFechaPago.EditValue = Date.Now.Date
    End Sub
End Class